package com.edu.controller;

import com.edu.dao.IndexDao;
import com.edu.dao.ProductDao;
import com.edu.data.*;
import com.edu.util.ActionResult;
import com.edu.util.Validator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/Cart")
//@SessionAttributes(value={"loginUser","cart","total"})
public class CartController {
    ShoppingCart sc = new ShoppingCart();
    protected Map<String, ActionResult> viewMapping = new HashMap<String, ActionResult>();
    @Autowired
    ProductDao productDao;

    //商品数量加
    @RequestMapping("/add")
    private String add(HttpServletRequest req,int id)throws SQLException{
//        String idStr=req.getParameter("entityId"); //商品id
        int enId=id;
        String quantityStr=req.getParameter("quantity"); //商品数量
        long quantity=1; //默认数量
        if(quantityStr!=null){
            quantity=Long.parseLong(quantityStr);
        }
        Product product= productDao.getDetails(enId); //根据商品id查询信息
        ShoppingCart cart=getCartFromSession(req);
        if(cart!=null){
            cart.addItem(product,quantity);
        }else {
            cart=new ShoppingCart();
        }
        req.getSession().setAttribute("cart",cart);
        req.setAttribute("msg","已经添加到购物车");
        return "../../shopping";
    }
    //跳转到购物车页面
    @RequestMapping("/list")
    private String list(HttpServletRequest request){
        getCartFromSession(request); //从seeion中获取购物车
        return "../../shopping"; //跳转到购物车页面
    }



    //添加用户的新收货地址
//    private String addAddress(HttpServletRequest request) {
//        String address=request.getParameter("address");
//        //点击"添加新地址"时传的中文乱码处理
//        if(request.getMethod().equals("GET") && address!=null){
//            try {
//                address = new String (address.getBytes("iso-8859-1"),"utf-8");
//            } catch (UnsupportedEncodingException e) {
//                e.printStackTrace();
//            }
//        }
//        UserService userService=new UserServiceImpl();
//        User user = getUserFromSession(request);
//        userService.addAdress(user, address);//向数据库添加新地址
//        user.setAddress(user.getAddress()+";"+address);//修改用户地址
//        return "addAddressSuccess";
//    }
//    //寻找此用户的所有收货地址
//    private String address(HttpServletRequest request) {
//        User user = getUserFromSession(request);
//        if(user==null)
//            return "login";
//        String address=user.getAddress();
//        String []add=address.split(";");
//        request.setAttribute("address", add);
//        return "addressSuccess";
//    }
//    //购买
//    private String pay(HttpServletRequest request) {
//        ShoppingCart cart = getCartFromSession(request);
//        User user = getUserFromSession(request);
//        if(user==null)
//            return "login";
//        String address=(String)request.getParameter("address");
//        orderService.payShoppingCart(cart, user,address);
//        removeCartFromSession(request);
//        request.setAttribute("msg", "恭喜：购买成功！");
//        return "paySuccess";
//    }

    private void removeCartFromSession(HttpServletRequest request) {
        //结账后清空购物车
        request.getSession().removeAttribute("cart");
        request.getSession().removeAttribute("cart2");
    }

    private User getUserFromSession(HttpServletRequest request) {
        HttpSession session = request.getSession();
        return (User) session.getAttribute("loginUser");
    }

//    private String add(HttpServletRequest request) {
//        //若用户尚未登录则不能购买商品
//        String id = request.getParameter("entityId");
//        String quantityStr = request.getParameter("quantity");
//        long quantity = 1;
//        if (!Validator.isEmpty(quantityStr))
//            quantity = Long.parseLong(quantityStr);
//        Product product = productService.findById(id);
//        ShoppingCart cart = getCartFromSession(request);
//        cart.addItem(product, quantity);
//        request.setAttribute("msg", "已经添加到购物车！");
//        return "addSuccess";
//    }
    //编辑
    private String mod(HttpServletRequest request) {
        String id = request.getParameter("entityId");
        String quantityStr = request.getParameter("quantity");
        long quantity = 1;
        if (!Validator.isEmpty(quantityStr))
            quantity = Long.parseLong(quantityStr);
        String indexStr = request.getParameter("index");
        ShoppingCart cart = getCartFromSession(request);
        cart.modifyQuantity(Integer.parseInt(indexStr), quantity);
        return "modSuccess";
    }
    //删除
    private String remove(HttpServletRequest request) {
        String id = request.getParameter("entityId");
        String quantityStr = request.getParameter("quantity");
        long quantity = 1;
        if (!Validator.isEmpty(quantityStr))
            quantity = Long.parseLong(quantityStr);
        String indexStr = request.getParameter("index");
        ShoppingCart cart = getCartFromSession(request);
        cart.getItems().remove(Integer.parseInt(indexStr));
        return "removeSuccess";
    }

    private ShoppingCart getCartFromSession(HttpServletRequest request) {
        HttpSession session = request.getSession();
        ShoppingCart cart = (ShoppingCart) session.getAttribute("cart");
        double total=0;//计算总价格
        for(int i=0;cart!=null&&i<cart.getItems().size();i++){
            ShoppingCartItem item=cart.getItems().get(i);
            total+=(item.getCost()*item.getQuantity());
        }
        //获取当前页
        String page = request.getParameter("page");
        int pageNo = 1;
        if (!Validator.isEmpty(page))
            pageNo = Integer.parseInt(page);
        if (cart == null) {
            cart = new ShoppingCart();
            session.setAttribute("cart", cart);
        }else{//获取本页要显示的内容
            ShoppingCart cart2 = new ShoppingCart();//获取购物车当前页商品
            cart2.items=new ArrayList<ShoppingCartItem> ();
            Pager pager = new Pager((cart.items.size()+1)/2,1, pageNo);
            for(int i=(pageNo-1)*2;i<(pageNo)*2&&i<cart.getItems().size();i++){//从购物车中选出本页商品
                cart2.items.add(cart.getItems().get(i));
            }
            request.setAttribute("pageNo", pageNo);//当前页
            session.setAttribute("cart2", cart2);//当前页显示的商品
            session.setAttribute("cart", cart);//全部的商品
            request.setAttribute("total", total);//总价格
            request.setAttribute("pager", pager);
        }
        return cart;
    }

//    private String list(HttpServletRequest request) {
//        getCartFromSession(request);
//        return "listSuccess";
//    }

//    private void prepareCategories(HttpServletRequest request) {
//        List<ProductCategory> categories = productService
//                .getProductCategories(null);
//        request.setAttribute("categories", categories);
//    }
//
//    private void prepareNews(HttpServletRequest request) {
//        List<News> allNews = facilityService.getAllNews(new Pager(10, 1));
//        request.setAttribute("allNews", allNews);
//    }

    protected void createViewMapping() {
        this.addMapping("listSuccess", "shopping.jsp");
        this.addMapping("paySuccess", "shopping-put.jsp");
        this.addMapping("addSuccess", "shopping-put.jsp");
        this.addMapping("removeSuccess", "Cart", true);
        this.addMapping("modSuccess", "Cart", true);
        this.addMapping("login", "login.jsp");
        this.addMapping("addressSuccess", "address.jsp");
        this.addMapping("addAddressSuccess", "Cart?action=address", true);
    }

    private void toView(HttpServletRequest req, HttpServletResponse resp, String result) throws IOException, ServletException {
        ActionResult dest = this.viewMapping.get(result);
        if (dest.isRedirect()) {
            resp.sendRedirect(dest.getViewName());
        } else {
            req.getRequestDispatcher(dest.getViewName()).forward(req, resp);
        }
    }

    protected void addMapping(String viewName, String url) {
        this.viewMapping.put(viewName, new ActionResult(url));
    }

    protected void addMapping(String viewName, String url, boolean isDirect) {
        this.viewMapping.put(viewName, new ActionResult(url, isDirect));
    }

}
